package com.techthinker.dreamBuilder.Dictionary;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import com.techthinker.dreamBuilder.Database.DatabaseHelper;
import com.techthinker.dreamBuilder.Define.Constant;
import com.techthinker.dreamBuilder.Entity.Dictionary;

public class DictionaryDAL {
	// private static final String TAG = "DictionaryService";
	private DatabaseHelper mDbHelper;
	private SQLiteDatabase mDb;
	private final Context mCtx;
	private static final String KEY_NAME = "name";
	private static final String KEY_VERSION = "version";
	private static final String KEY_DESCRIPTION = "description";
	private static final String KEY_SIZE = "size";
	private static final String KEY_LISTSNUM = "listsNum";
	private static final String KEY_WORDSNUM = "wordsNum";
	private static final String KEY_ISNOTEBOOK = "isNotebook";
	private static final String KEY_ROWID = "dictionary_id";

	public DictionaryDAL(Context ctx) {
		this.mCtx = ctx;
	}

	public DictionaryDAL open() throws SQLException {
		mDbHelper = new DatabaseHelper(mCtx, Constant.DB_NAME);
		mDb = mDbHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		mDbHelper.close();
	}

	public boolean isClosed() {
		if (mDbHelper == null)
			return true;
		else
			return false;
	}

	// create a dictionary item
	public long createDictionary(Dictionary dictionary) {
		ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_NAME, dictionary.getName());
		initialValues.put(KEY_VERSION, dictionary.getVersion());
		initialValues.put(KEY_DESCRIPTION, dictionary.getDescription());
		initialValues.put(KEY_SIZE, dictionary.getSize());
		initialValues.put(KEY_LISTSNUM, dictionary.getListsNum());
		initialValues.put(KEY_WORDSNUM, dictionary.getWordsNum());
		initialValues.put(KEY_ISNOTEBOOK, dictionary.isNotebook());
		return mDb.insert(Constant.TABLE_DICTIONARY, null, initialValues);
	}

	// update a dictionary item
	public boolean updateDictionary(Dictionary dictionary) {
		ContentValues args = new ContentValues();
		args.put(KEY_NAME, dictionary.getName());
		args.put(KEY_VERSION, dictionary.getVersion());
		args.put(KEY_DESCRIPTION, dictionary.getDescription());
		args.put(KEY_SIZE, dictionary.getSize());
		args.put(KEY_LISTSNUM, dictionary.getListsNum());
		args.put(KEY_WORDSNUM, dictionary.getWordsNum());
		args.put(KEY_ISNOTEBOOK, dictionary.isNotebook());
		return mDb.update(Constant.TABLE_DICTIONARY, args, KEY_ROWID + "="
				+ dictionary.getDictionary_id(), null) > 0;
	}

	// delete a dictionary item and it's related lists, words
	public boolean deleteDictionary(Dictionary dictionary) {
		return deleteDictionary(dictionary.getDictionary_id());
	}

	// delete a dictionary item and it's related lists, words
	public boolean deleteDictionary(int dictionary_id) {
		// fetch the related list
		ListDAL listDAL = new ListDAL(mCtx);
		listDAL.open();
		listDAL.deleteListsByDictionary(dictionary_id);
		listDAL.close();
		return mDb.delete(Constant.TABLE_DICTIONARY, KEY_ROWID + "="
				+ dictionary_id, null) > 0;
	}

	// retrieve all dictionaries
	public List<Dictionary> fetchAllDictionarys() {
		Cursor mCursor = mDb.query(Constant.TABLE_DICTIONARY, new String[] {
				KEY_ROWID, KEY_NAME, KEY_VERSION, KEY_DESCRIPTION, KEY_SIZE,
				KEY_LISTSNUM, KEY_WORDSNUM, KEY_ISNOTEBOOK }, null, null, null,
				null, null);
		List<Dictionary> allDicList = new ArrayList<Dictionary>();
		while (mCursor.moveToNext()) {
			Dictionary dictionary = new Dictionary();
			dictionary.setDictionary_id(mCursor.getInt(mCursor
					.getColumnIndex(KEY_ROWID)));
			dictionary.setName(mCursor.getString(mCursor
					.getColumnIndex(KEY_NAME)));
			dictionary.setVersion(mCursor.getString(mCursor
					.getColumnIndex(KEY_VERSION)));
			dictionary.setDescription(mCursor.getString(mCursor
					.getColumnIndex(KEY_DESCRIPTION)));
			dictionary.setSize(mCursor.getFloat(mCursor
					.getColumnIndex(KEY_SIZE)));
			dictionary.setListsNum(mCursor.getInt(mCursor
					.getColumnIndex(KEY_LISTSNUM)));
			dictionary.setWordsNum(mCursor.getInt(mCursor
					.getColumnIndex(KEY_WORDSNUM)));
			dictionary.setNotebook(Boolean.parseBoolean(mCursor
					.getString(mCursor.getColumnIndex(KEY_WORDSNUM))));
			allDicList.add(dictionary);
		}
		mCursor.close();
		return allDicList;
	}

	// check if the dictionary is installed
	public boolean dicIsInstalled(String dicName, String verName) {
		Cursor mCursor = mDb.query(true, Constant.TABLE_DICTIONARY,
				new String[] { KEY_ROWID, KEY_NAME, KEY_VERSION,
						KEY_DESCRIPTION, KEY_SIZE, KEY_LISTSNUM, KEY_WORDSNUM,
						KEY_ISNOTEBOOK }, KEY_NAME + "='" + dicName + "' and "
						+ KEY_VERSION + "='" + verName + "'", null, null, null,
				null, null);
		if (mCursor.moveToFirst())
			return true;
		else
			return false;
	}

	// retrieve all dictionaries to cursor
	public Cursor fetchAllDictionarysToCursor() {
		return mDb.query(Constant.TABLE_DICTIONARY, new String[] { KEY_ROWID,
				KEY_NAME, KEY_VERSION, KEY_DESCRIPTION, KEY_SIZE, KEY_LISTSNUM,
				KEY_WORDSNUM, KEY_ISNOTEBOOK }, null, null, null, null, null);
	}

	// retrieve a dictionary item by id
	public Dictionary fetchDictionaryById(long rowId) throws SQLException {

		Cursor mCursor = mDb.query(true, Constant.TABLE_DICTIONARY,
				new String[] { KEY_ROWID, KEY_NAME, KEY_VERSION,
						KEY_DESCRIPTION, KEY_SIZE, KEY_LISTSNUM, KEY_WORDSNUM,
						KEY_ISNOTEBOOK }, KEY_ROWID + "=" + rowId, null, null,
				null, null, null);
		if (mCursor.moveToFirst()) {
			Dictionary dictionary = new Dictionary();
			dictionary.setDictionary_id(mCursor.getInt(mCursor
					.getColumnIndex(KEY_ROWID)));
			dictionary.setName(mCursor.getString(mCursor
					.getColumnIndex(KEY_NAME)));
			dictionary.setVersion(mCursor.getString(mCursor
					.getColumnIndex(KEY_VERSION)));
			dictionary.setDescription(mCursor.getString(mCursor
					.getColumnIndex(KEY_DESCRIPTION)));
			dictionary.setSize(mCursor.getFloat(mCursor
					.getColumnIndex(KEY_SIZE)));
			dictionary.setListsNum(mCursor.getInt(mCursor
					.getColumnIndex(KEY_LISTSNUM)));
			dictionary.setWordsNum(mCursor.getInt(mCursor
					.getColumnIndex(KEY_WORDSNUM)));
			dictionary.setNotebook(Boolean.parseBoolean(mCursor
					.getString(mCursor.getColumnIndex(KEY_WORDSNUM))));
			mCursor.close();
			return dictionary;
		}
		mCursor.close();
		return null;

	}
}
